package com.sgb.kjwl.view.ui.activity.self.packers;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.bumptech.glide.Glide;
import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.AtSelfTeamEmploymentBinding;
import com.sgb.kjwl.di.self.DaggerSelfComponent;
import com.sgb.kjwl.di.self.SelfModule;
import com.sgb.kjwl.model.entity.EmploymentEntity;
import com.sgb.kjwl.model.entity.self.SelfEmploymentEntity;
import com.sgb.kjwl.view.ui.adapter.self.packers.SelfTeamEmploymentAdapter;
import com.sgb.kjwl.viewmodel.self.packers.SelfTeamEmploymentModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.dialog.CommonDialog;
import com.swgk.core.helper.OnMultiClickListener;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.xrecyclerview.XRecyclerView;

import java.util.List;

import javax.inject.Inject;

/**
 * description:灵活用工
 * @author liWei
 * @date 2020/11/10
 */
public class SelfTeamEmploymentActivity extends AppActivity {

    public static final int REQUEST_CODE = 10012;

    private AtSelfTeamEmploymentBinding binding;

    @Inject
    SelfTeamEmploymentModel viewModel;

    private SelfTeamEmploymentAdapter adapter;

    public static void start(Context context) {
        Intent intent = new Intent();
        intent.setClass(context, SelfTeamEmploymentActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected boolean statusBarDark() {
        return false;
    }

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerSelfComponent.builder().appComponent(appComponent).selfModule(new SelfModule(this)).build().inject(this);
    }

    @Override
    protected void onCreate(@Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mActionBar.setMenuFunctionClick(new OnMultiClickListener() {
            @Override
            public void onMultiClick(View v) {
                SelfTeamEmploymentEditActivity.start(SelfTeamEmploymentActivity.this, REQUEST_CODE);
            }
        });
    }

    @Override
    protected void initView() {
        binding = DataBindingUtil.setContentView(this, R.layout.at_self_team_employment);
        binding.setViewModel(viewModel);
        if (adapter == null) {
            adapter = new SelfTeamEmploymentAdapter(this, null, Glide.with(this));
        }
        adapter.setItemCallBack(new SelfTeamEmploymentAdapter.ItemCallBack() {
            @Override
            public void del(String no) {
                optState(no);
            }

            @Override
            public void edit(String no) {
                SelfTeamEmploymentEditActivity.start(SelfTeamEmploymentActivity.this, no, REQUEST_CODE);
            }
        });
        adapter.setOnItemClick(new AdapterOnItemClick<EmploymentEntity>() {
            @Override
            public void onItemClick(EmploymentEntity selfEmploymentEntity, int position) {
                viewModel.getH5Url(selfEmploymentEntity.getNo());
            }
        });
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        binding.recyclerView.setLayoutManager(layoutManager);
        binding.recyclerView.setAdapter(adapter);
        binding.recyclerView.setLoadingMoreEnabled(true);
        binding.recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                viewModel.getEmploymentList(false);
            }

            @Override
            public void onLoadMore() {
                viewModel.getEmploymentList(true);
            }
        });
    }

    @Override
    protected void init() {
        viewModel.getEmploymentList(false);
    }

    public void showList(List<EmploymentEntity> list, boolean isLoadMore, boolean isLast) {
        binding.recyclerView.refreshComplete();
        if (isLoadMore) {
            binding.recyclerView.loadMoreComplete();
            if (list != null && list.size() > 0) {
                adapter.addDatas(list);
            }
            binding.recyclerView.setNoMore(isLast);
        } else {
            if (null != list && list.size() > 0) {
                binding.emptyView.setVisibility(View.GONE);
                adapter.setDatas(list);
                binding.recyclerView.setLoadingMoreEnabled(true);
            } else {
                adapter.setDatasMayNull(list);
                binding.emptyView.setVisibility(View.VISIBLE);
            }
        }
    }

    private void optState(String no) {
        new CommonDialog(getActivity()).builder().setPositiveButton("确定").setNegativeButton("取消")
                .setTitle("确认删除？")
                .setDialogCallBack(new CommonDialog.DialogCallBack() {
                    @Override
                    public void onRightClick() {
                        viewModel.delEmployment(no);
                    }

                    @Override
                    public void onLeftClick() {

                    }
                }).show();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
        super.onActivityResult(requestCode, resultCode, intent);
        if (resultCode == RESULT_OK) {
            if (requestCode == REQUEST_CODE) {
                viewModel.getEmploymentList(false);
            }
        }
    }
}
